package com.example.music_project.tools;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

//@ControllerAdvice
//public class ResponseHandle implements ResponseBodyAdvice {
//    private ObjectMapper objectMapper = new ObjectMapper();
//    private Result<Object> result = new Result<>();
//
//    @Override
//    public boolean supports(MethodParameter returnType, Class converterType) {
//        //所有接口都进行统一结果返回
//        return true;
//        /*
//        可以手动指定哪些接口不用统一结果。
//         */
//    }
//
//    @Override
//    @SneakyThrows // 会自动生成try catch
//    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
//
//        //判断body是否是Result
//        if(body instanceof Result) {
//            return body;
//        }
//        if(body instanceof String) {
//            //Sting类型单独处理
//            return result.success(objectMapper.writeValueAsString(body));
//        }
//        return result.success(body);
//    }
//}
